import sys 
import os.path

from aria_property_mappings import aria_property_mappings
from aria_state_mappings    import aria_state_mappings
from aria_role_mappings     import aria_role_mappings
from wai_aria_definitions   import aria_global_properties, aria_property_types, aria_abstract_roles, aria_design_patterns

def html_head_start( f, path, title ):
  f.write("<!DOCTYPE html>\n")
  f.write("<html>\n")
  f.write("  <head>\n")
  f.write("    <title>" + title + "</title>\n")
  f.write("    <link rel='stylesheet' href='" + path + "css/aria_tests.css' type='text/css' media='all'>\n")
  f.write("    <script src='" + path + "js/aria_constants.js' type='text/javascript'></script>\n")

def html_head_style_link( f, path, filename, media ):
  f.write("    <link rel='stylesheet' href='" + path + "css/" + filename + "' type='text/css' media='" + media + "'>\n")

def script_reference( f, path, filename ):
  f.write("    <script src='" + path + "js/" + filename + "' type='text/javascript'></script>\n")

def script_set_focus( f, id ):
  f.write("    <script type='text/javascript'>\n")
  f.write("      function setFocus() { \n")
  f.write("        node = document.getElementById('" + id + "');\n")
  f.write("        node.focus();\n")
  f.write("      } // end onload function \n")  
  f.write("    </script>\n")
  
def script_add_results_ref( f, fname_results ):
  f.write("    <script src='../js/jquery.js'            type='text/javascript'></script>\n")
  f.write("    <script src='../js/aria_tests.js'        type='text/javascript'></script>\n")
  f.write("    <script src='../js/show_test_results.js' type='text/javascript'></script>\n")
  f.write("    <script src='" + fname_results + "' type='text/javascript'></script>\n")
  
def html_body_start( f, id, title, test ):
  f.write("  </head>\n")
  f.write("  <body id='" + id + "' onload='setFocus()'>\n")
  f.write("    <div class='nav'>\n")
  f.write("      <ul role='navigation' aria-label='" + test['role'] + " test navigation'>\n")
  f.write("        <li><a href='../index.html'>All Tests</a></li>\n")  
  f.write("        <li><a href='index.html'>" + test['role'] + " Tests</a></li>\n")  
  f.write("      </ul>\n")
  f.write("    </div>\n")
  f.write("    <h1>" + title + "</h1>\n")
  f.write("    <p>Test ID: " + id + "</p>\n")
  f.write("    <p class='description'><strong>Description: </strong>" + test['description'] + "</p>\n")

def html_element_test_code(f, test ):
  f.write("    <" + test['element'] + " id='test' tabindex='0'\n")
  f.write("      role='" + test['role'] + "'\n") 
  
  # Check for aria-describedby attribute
  if test['aria-describedby']:
    f.write("      aria-describedby='test_desc'\n")      
  
  # Add attribute content related to the element
  for attribute in test['attributes']:
    [a, value] = attribute.split('=')
    f.write("      " + a + "='" + value + "'\n")
    
  # Add related ARIA Properties
  for property in test['properties']:
    [p, value] = property.split('=')
    f.write("      " + p + "='" + value + "'\n")
    
  # Add related ARIA States
  for state in test['states']:
    [s, value] = state.split('=')
    f.write("      " + s + "='" + value + "'\n")
    
  # Check for title attribute
  if test['title']:
    f.write("      title='" + test['title'] + "'\n")      

  # Check for aria-label attribute
  if test['aria-label']:
    f.write("      aria-label='" + test['aria-label'] + "'\n")      

  # Check for aria-labelledy references
  alabel_list = test['aria-labelledby']
  if len(alabel_list):
    f.write("      aria-labelledby='")
    i = 1    
    for item in alabel_list:
      f.write("alabel_" + str(i) + " " )      
      i += 1
    if test['include-node']:
      f.write("test " )      
    
    f.write("'\n")      

  f.write("      >\n")
  
  f.write("      " + test['content'] + "\n")
  f.write("    </" + test['element'] + ">\n")

  # if any aria-describedby content, add it to the page
  if test['aria-describedby']:
    f.write("    <p id='test_desc'>" + test['aria-describedby'] + "</p>\n")

  # Check for aria-labelledy references
  if len(alabel_list):
    i = 1
    for item in alabel_list:
      f.write("   <div id='alabel_" + str(i) + "'>" + item + "</div>\n")      
      i += 1
      
def empty_string_test( str ):
  str = str.strip()
  if not str:
    str = "none"
  return str  
      
def html_api_value_mappings(f, value, mappings ):

  f.write("        <tr>\n")
  f.write("          <th>\n")   
  f.write("            " + value + "\n")  
  f.write("          </th>\n")  
  
  # MSAA
  f.write("          <td>\n")  
  f.write("            <ul>\n") 
  for item in mappings['MSAA']:
    item = empty_string_test( item )
    f.write("            <li>" + item + "\n")  
  f.write("            </ul>\n")  
  f.write("          </td>\n")  

  # IAccessible2
  f.write("          <td>\n")  
  f.write("            <ul>\n")  
  for item in mappings['IAccessible2']:
    item = empty_string_test( item )
    f.write("            <li>" + item + "\n")  
  f.write("            </ul>\n")  
  f.write("          </td>\n")  

  # UIA
  f.write("          <td>\n")  
  f.write("            <ul>\n")  
  for item in mappings['UIA']:
    item = empty_string_test( item )
    f.write("            <li>" + item + "\n")  
  f.write("            </ul>\n")  
  f.write("          </td>\n")  

  # ATK
  f.write("          <td>\n")  
  f.write("            <ul>\n")  
  for item in mappings['ATK']:
    item = empty_string_test( item )
    f.write("            <li>" + item + "\n")  
  f.write("            </ul>\n")  
  f.write("          </td>\n")  

  # MAC
  f.write("          <td>\n")  
  f.write("            <ul>\n")  
  for item in mappings['MAC']:
    item = empty_string_test( item )
    f.write("            <li>" + item + "\n")
  f.write("            </ul>\n")  
  f.write("          </td>\n")  

  f.write("        </tr>\n")


def html_api_mappings(f, test):
  f.write("    <h2>Accessibility API Mappings</h2>\n")
  f.write("    <table class='api' border='1px' summary='Accessibility API Mappings'>\n")
  f.write("      <caption>Accessibility API Mappings</caption>\n")
  f.write("      <thead>\n")
  f.write("        <th>Value</th>\n")
  f.write("        <th>MSAA</th>\n")
  f.write("        <th>IAccessible2</th>\n")
  f.write("        <th>UIA</th>\n")
  f.write("        <th>ATK</th>\n")
  f.write("        <th>MAC</th>\n")
  f.write("      </thead>\n")
  
  f.write("      <tbody>\n")

  html_api_value_mappings(f, "Role=" + test['role'], aria_role_mappings[test['role']])
  
  for state in test['states']:
    [s, v] = state.split('=')
    mapping = {}
    for aria_state_item in aria_state_mappings:
       if aria_state_item['state'] == s and aria_state_item['value'] == v:  
         mapping = aria_state_item['description']
         break
        
    html_api_value_mappings(f, s + "='" + v + "'<br/>(State)", mapping)

  for property in test['properties']:
    [p, v] = property.split('=')
    mapping = {}
    for aria_property_item in aria_property_mappings:
       if aria_property_item['property']==p:  
         mapping = aria_property_item['description']
         break
    
    print p + "=" + v + '(property)'   
    html_api_value_mappings(f, p + "='" + v + "'<br/>(Property)", mapping)


  f.write("      </tbody>\n")
  f.write("    </table>\n")

def accessible_name( test ):

  name = ""

  labels  = test['aria-labelledby']  
  flag    = test['include-node']
  title   = test['title']
  label   = test['aria-label']
  content = test['content']

  if label:
    name += label + " " 
  else:
    if not len(labels):
      if title:
        name = title
      else: 
        name = content

  if len(labels):
    for l in labels:
      name1 = l + " " 
  
    if flag:
      name = name1 + name
    else:
      name = name1    
      
  return name


def html_accessible_name(f, test):
  f.write("      <h2>Accessible Name</h2>\n")  
  
  f.write("        <p><a href='http://www.w3.org/TR/wai-aria/roles#namecalculation'>Information on accessible name calculation</a>.</p>\n")
  f.write("        <p class='name'><strong>Calculated Accesible Name: </strong><span>" + accessible_name( test) + "</span></p>\n")
  
  f.write("        <table class='name' border='1px' summary='Sources of Accessible Name for Widget'>\n")  
  f.write("          <caption>Sources of Accessible Name for Widget</caption>\n")  
  
  f.write("          <thead>\n")  
  f.write("            <tr>\n")  
  f.write("              <th>Source</th>\n")  
  f.write("              <th>Value</th>\n")  
  f.write("            </tr>\n")  
  f.write("          </thead>\n")  
  f.write("          <tbody>\n")  
  
  if test['content']:
    f.write("            <tr>\n")  
    f.write("              <th>Child node text content</th>\n")      
    f.write("              <td>" + test['content'] + "</td>\n")      
    f.write("            </tr>\n")  
    
  if test['aria-label']:
    f.write("            <tr>\n")  
    f.write("              <th>aria-label attribute</th>\n")      
    f.write("              <td>" + test['aria-label'] + "</td>\n")      
    f.write("            </tr>\n")  
    
  labels = test['aria-labelledby']  
  if len(labels):
    for (index, label) in enumerate(labels):
      f.write("            <tr>\n")  
      f.write("              <th>aria-labelledby reference " + str(index+1) + "</th>\n")      
      f.write("              <td>" + label + "</td>\n")      
      f.write("            </tr>\n")  
      
  labels = test['html-label']  
  if len(labels):
    for (index, label) in enumerate(labels):
      f.write("            <tr>\n")  
      f.write("              <th>Label element " + str(index+1) + "</th>\n")      
      f.write("              <td>" + label + "</td>\n")      
      f.write("            </tr>\n")  
            
  if test['title']:
    f.write("            <tr>\n")  
    f.write("              <th>title attribute</th>\n")      
    f.write("              <td>" + test['title'] + "</td>\n")      
    f.write("            </tr>\n")  
    
  f.write("          </tbody>\n")  
  f.write("        </table>\n")  

def html_body_end(f):
  f.write("  </body>\n")  
  f.write("</html>\n")


def make_results_file( s, id, path, fname, title, desc, type, feature, test, group ):
  s.write("var result         = {};\n\n")
  s.write("result.id          = '" + id      + "';\n")
  s.write("result.group       = '" + str(group)   + "';\n")
  s.write("result.file        = '" + fname   + "';\n")
  s.write("result.path        = '" + path    + "';\n")
  s.write("result.type        = '" + type    + "';\n")
  s.write("result.feature     = '" + feature + "';\n")
  s.write("result.title       = '" + title   + "';\n")
  s.write("result.description = '" + desc    + "';\n")
  s.write("result.properties  = [")
  # Add related ARIA Properties
  for property in test['properties']:
    s.write("'" + property +"',")    
  s.write("];\n")
    
  s.write("result.states      = [")
  # Add related ARIA States
  for state in test['states']:
    s.write("'" + state + "',")
  s.write("];\n")

  if test['aria-label']:
    s.write("result.aria_label      = true;\n")
  else:
    s.write("result.aria_label      = false;\n")

  if len(test['aria-labelledby']):
    s.write("result.aria_labelledby = true;\n")
  else:
    s.write("result.aria_labelledby = false;\n")

  if len(test['html-label']):
    s.write("result.label_element   = true;\n")
  else:
    s.write("result.label_element   = false;\n")

  if test['title']:
    s.write("result.title_attribute = true;\n")
  else:
    s.write("result.title_attribute = false;\n")

  s.write("\n\n// Create results object \n")
  s.write("result.results = {};\n")

  s.write("\n// Windows 7 MSAA and IAccessible2 user agent testing results \n")
  s.write("result.results.win7 =[];\n")
  
  s.write("result.results.win7.push(  {'name': 'firefox_4'    , result: 'u', accessible_name: 'u', comment: ''});\n")  
  s.write("result.results.win7.push(  {'name': 'ie_9'         , result: 'u', accessible_name: 'u', comment: ''});\n")
  s.write("result.results.win7.push(  {'name': 'safari_5'     , result: 'u', accessible_name: 'u', comment: ''});\n")
  s.write("result.results.win7.push(  {'name': 'chrome_10'    , result: 'u', accessible_name: 'u', comment: ''});\n")
  s.write("result.results.win7.push(  {'name': 'opera_11'     , result: 'u', accessible_name: 'u', comment: ''});\n")

  s.write("\n\n// Windows 7 UI Automation agent testing results \n")
  s.write("result.results.win7uia =[];\n")
  s.write("result.results.win7uia.push(  { 'name': 'ie_9', result: 'u', accessible_name: 'u', comment: ''});\n")

  s.write("\n\n// OS X 10.6 user agent testing results \n")
  s.write("result.results.osx106 =[];\n")
  s.write("result.results.osx106.push({ 'name': 'safari_5',  result: 'u', accessible_name: 'u', comment: ''});\n")
  s.write("result.results.osx106.push({ 'name': 'firefox_4', result: 'u', accessible_name: 'u', comment: ''});\n")
  s.write("result.results.osx106.push({ 'name': 'chrome_10', result: 'u', accessible_name: 'u', comment: ''});\n")
  s.write("result.results.osx106.push({ 'name': 'opera_11',  result: 'u', accessible_name: 'u', comment: ''});\n")
  
  s.write("\n\n// Unix/Linux user agent testing results \n")
  s.write("result.results.unix =[];\n")
  s.write("result.results.unix.push(  { 'name': 'firefox_4', result: 'u', accessible_name: 'u', comment: ''});\n")

  s.write("\n\n// Screen Reader Support \n")
  s.write("result.results.sr =[];\n")
  s.write("result.results.sr.push(  {'name': 'jaws_12'      , result: 'u', accessible_name: 'u', comment: ''});\n")
  s.write("result.results.sr.push(  {'name': 'windoweyes_71', result: 'u', accessible_name: 'u', comment: ''});\n")
  s.write("result.results.sr.push(  {'name': 'ndva',          result: 'u', accessible_name: 'u', comment: ''});\n")
  s.write("result.results.sr.push(  {'name': 'voiceover',     result: 'u', accessible_name: 'u', comment: ''});\n")


  s.write("\naria_test_results.add( result )\n")

def make_element_test( group, id, title, path, fname, test):
  print "\nCreating  " + path + fname + "...\n"
    
  fname_test    = "test_" + fname + ".html";
  fname_results = "results_" + fname + ".js";
  
  f = open(path+fname_test,    'w')
  
  if not os.path.isfile(path+fname_results):
    s = open(path+fname_results, 'w')
    make_results_file( s, id, path, fname_test, title, test['description'], 'role', test['role'], test, group )

  html_head_start(f, "../", title)
  if test['stylesheet']:
    html_head_style_link( f, "../", test['stylesheet'], 'all' )
  script_set_focus( f, 'test' )
  script_add_results_ref( f, fname_results )
  html_body_start(f, id, title, test)
  html_element_test_code(f, test)
  html_accessible_name(f, test)
  html_api_mappings(f, test)
  html_body_end(f)
  return fname_results  

def make_group_index_file( path, files, group, title ):

  f = open(path+'index.html','w')

  html_head_start(f, "../", title)

  f.write("    <script src='../js/jquery.js'             type='text/javascript'></script>\n")
  f.write("    <script src='../js/aria_tests.js'         type='text/javascript'></script>\n")
  f.write("    <script src='../js/show_group_results.js' type='text/javascript'></script>\n")
  
  for file in files:
    f.write("    <script src='" + file + "' type='text/javascript'></script>\n")
  
  f.write("  </head>\n")
  f.write("  <body>\n")
  f.write("    <div class='nav'>\n")
  f.write("      <ul role='navigation' aria-label='Main Navigation'>\n")
  f.write("        <li><a href='../index.html'>All Tests</a></li>\n")  
  f.write("      </ul>\n")
  f.write("    </div>\n")

  f.write("    <h1>" + title + "</h1>\n")
  f.write("    <div id='content' role='main'>\n")
  f.write("    </div>\n")
  html_body_end(f)

  # Group  Results file
  fname = path + "results_group_" + str(group).zfill(3) + ".js"
  f = open(fname,'w')
  f.write("var group         = {};\n\n")
  f.write("group.num         = '" + str(group) + "';\n")
  f.write("group.title       = '" + title      + "';\n")
  f.write("group.url         = '" + path  + "index.html';\n")
  f.write("\naria_test_results.groups.push( group );\n")
  return fname


def make_all_index_file( files, title ):

  f = open('index.html','w')

  html_head_start(f, "", title)

  f.write("    <script src='js/jquery.js'             type='text/javascript'></script>\n")
  f.write("    <script src='js/aria_tests.js'         type='text/javascript'></script>\n")
  f.write("    <script src='js/show_all_results.js' type='text/javascript'></script>\n")
  
  for file in files:
    f.write("    <script src='" + file + "' type='text/javascript'></script>\n")
  
  f.write("  </head>\n")
  f.write("  <body>\n")
  f.write("    <h1>ARIA Test Results</h1>\n")
  f.write("    <div id='content' role='main'>\n")
  f.write("      <h2>Test Group Summary Results</h2>\n")  
  f.write("      <div id='group'></div>\n")
  f.write("      <h2>All Test Results</h2>\n")  
  f.write("      <div id='all'></div>\n")  
  f.write("    </div>\n")
  html_body_end(f)

print("End of test creation")
  
  


